package com.future.controller;

import com.future.domain.DTO.MediaResourceDTO;
import com.future.domain.MediaResource;
import com.future.domain.vo.MediaResourceVO;
import com.future.domain.Result;
import com.future.service.MediaResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@Api("媒体资源")
@RestController
@RequestMapping("/media")
public class MediaResourceController {

    @Autowired
    private MediaResourceService mediaResourceService;

    @ApiOperation("上传文件作为预览")
    @PostMapping("/upload")
    public Result uploadMedia(
            @RequestParam("file") MultipartFile file,
            @RequestParam("type") int type) {
        return mediaResourceService.uploadMedia(file,type);
    }

    @ApiOperation("删除预览文件")
    @DeleteMapping("/deleteMinioMedia")
    public Result deleteMinioMedia(String fileType,String filePath) {
        return mediaResourceService.deleteMinioMedia(fileType, filePath);
    }

    @ApiOperation("上传文件到数据库")
    @PostMapping("/uploadTODB")
    public Result uploadTODB(@RequestBody MediaResourceDTO mediaResourceDTO) {
        return mediaResourceService.uploadTODB(mediaResourceDTO);
    }
    @ApiOperation("删除文件")
    @DeleteMapping("/{id}")
    public Result deleteMedia(@PathVariable Long id) {
        return mediaResourceService.deleteMedia(id);
    }


    @ApiOperation("前台获取所有用户的fileType")
    @PostMapping("/type")
    public Result getByType(@RequestParam String fileType,
                            @RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "10") Integer pageSize) {
        return mediaResourceService.listByType(fileType, pageNum, pageSize);
    }

    @ApiOperation("获取当前用户的所有文件类型")
    @GetMapping("/user")
    public Result getByUser(@RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "10") Integer pageSize) {
        return mediaResourceService.listByUser(pageNum, pageSize);
    }

    @ApiOperation("获取当前用户的fileType")
    @PostMapping("/userAndType")
    public Result getByUserAndType(@RequestParam String fileType,
                                  @RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "10") Integer pageSize) {
        return mediaResourceService.listByUserAndType(fileType, pageNum, pageSize);
    }

    @ApiOperation("下载文件")
    @GetMapping("/download/{id}")
//    这里的响应体是 byte[] 类型，对应文件的二进制数据（通过 result.getData() 获取）。
//    客户端收到后，会将这些二进制数据解析为实际的文件（图片、视频等）。
    public ResponseEntity<byte[]> downloadMedia(@PathVariable Long id) {
        Result result = mediaResourceService.downloadMedia(id);
        if (result == null || !result.getSuccess()) {
            return ResponseEntity.notFound().build();
        }
        
        Result mediaResult = mediaResourceService.getMediaInfo(id);
        if (mediaResult == null || !mediaResult.getSuccess()) {
            return ResponseEntity.notFound().build();
        }
        MediaResourceVO media = (MediaResourceVO) mediaResult.getData();
        //Content-Disposition头告诉浏览器 "这是一个需要下载的文件"
        return ResponseEntity.ok()
            .header(HttpHeaders.CONTENT_DISPOSITION, 
                    "attachment; filename=\"" + media.getFileName() + "\"")
            .contentType(MediaType.APPLICATION_OCTET_STREAM)
            .body((byte[]) result.getData());
    }

//    @ApiOperation("下载大文件")
//    @GetMapping("/downloadBigFile/{id}")
//    public void downloadMedia(@PathVariable Long id, HttpServletResponse response) {
//        mediaResourceService.downloadBigMedia(id, response);
//    }

  @ApiOperation("按id查询单个媒体资源")
    @GetMapping("/{id}")
    public Result getMediaById(@PathVariable Long id) {
        return mediaResourceService.getMediaInfo(id);
    }

    @ApiOperation("当前用户按id修改媒体资源")
    @PutMapping("/{id}")
    public Result updateMediaById(@PathVariable Long id, @RequestBody MediaResourceDTO dto) {
        return mediaResourceService.updateMediaById(id, dto);
    }
}